package com.livery.app.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.border.Border;

import com.livery.app.controller.AbstractWorkspaceController;
import com.livery.app.model.AbstractWorkspaceModel;
import com.livery.app.model.observators.BookingObserver;
import com.livery.app.model.observators.CarObserver;
import com.livery.app.model.observators.ClientObserver;
import com.livery.app.model.observators.TransactionObserver;
import com.livery.app.view.frame.BookingFrame;
import com.livery.app.view.frame.CarFrame;
import com.livery.app.view.frame.ClientFrame;
import com.livery.app.view.frame.RepairFrame;
import com.livery.app.view.frame.TransactionFrame;
import com.livery.data.Booking;
import com.livery.data.Car;
import com.livery.data.Client;
import com.livery.data.Transaction;

public class WorkerView implements ActionListener{

	private AbstractWorkspaceModel model;
	private AbstractWorkspaceController controller;
	
	private JFrame clientFrame;
	private JFrame carFrame;
	private JFrame transactionFrame;
	private JFrame bookingFrame;
	private JFrame repairFrame;
	private JFrame view;
	
	private JToolBar toolBar;
	private JButton btnClient;
	private JButton btnCar;
	private JButton btnTransaction;
	private JButton btnBooking;
	private JButton btnRepair;
	private JButton btnLogOut;
	private JButton btnExit;
	private static Point point = new Point();
	public WorkerView(AbstractWorkspaceModel model, AbstractWorkspaceController controller){
		this.model = model;
		this.controller = controller;
		
	}
	
	public void createView(){
		view = new JFrame("Zarządzanie wypożyczalnią");
		view.setUndecorated(true);
		view.getContentPane().setBackground(Color.decode("0xe4e4e4"));
		createControls();
		view.setLayout(new GridBagLayout());
		GridBagConstraints gridConst = new GridBagConstraints();
		gridConst.insets = new Insets(10, 10, 10, 10);
		gridConst.gridx = 0;
		gridConst.gridy = 0;
		view.add(toolBar, gridConst);
		view.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
			        point.x = e.getX();
			        point.y = e.getY();
			      }
			    });
		view.addMouseMotionListener(new MouseMotionAdapter() {
			 public void mouseDragged(MouseEvent e) {
			        Point p = view.getLocation();
			        view.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
			      }
			    });
		view.pack();
		Dimension dialogSize = 	view.getSize();		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		if(dialogSize.height > screenSize.height) 
			dialogSize.height = screenSize.height;
		if(dialogSize.width > screenSize.width)
			dialogSize.height = screenSize.width;
		view.setLocation((screenSize.width-dialogSize.width)/2,   
						(screenSize.height-dialogSize.height)/2);
		
		view.setVisible(true);
	}

	private void createControls(){
		btnClient = createButton("Klienci", "client.png", 
				"Wyświetl informacje o klientach. Możliwość dodawania, edytowania, archiwizowania klientów");
		btnCar = createButton("Samochody", "car.png", 
				"Wyświetl informacje o samochodach. Możliwość dodawania, edytowania, archiwizowania samochodów");
		btnTransaction = createButton("Transakcje", "transaction.png", 
				"Wyświetl informacje o transakcjach. Możliwość dodawania, edytowania, finalizowania transakcji");
		btnBooking = createButton("Rezerwacje", "booking.png", 
				"Wyświetl informacje o rezerwacjach. Możliwość dodawania, edytowania, usuwania, archiwizowania rezerwacji");
		btnRepair = createButton("Naprawy", "repair.png", 
				"Wyświetl informacje jakie samochody są w naprawie. Możliwość dodawania, archiwizowania napraw");
		btnLogOut = createButton("Wyloguj", "logout.png", "Wyloguj");
		btnExit = createButton("Zamknij", "exit.png", "Zamknij aplikacje");
		
		toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.add(btnClient);
		toolBar.add(btnCar);
		toolBar.add(btnTransaction);
		toolBar.add(btnBooking);
		toolBar.add(btnRepair);
		toolBar.addSeparator();
		toolBar.add(btnLogOut);
		toolBar.add(btnExit);
		
		
	}
	private JButton createButton(String text, String path, String toolTip){
		JButton button;
		try{
			Icon icon = createIcon(path);
			button = new JButton(icon);
		}
		catch (Exception e) {
			button = new JButton(text);
		}
		button.setToolTipText(toolTip);
		button.addActionListener(this);
		button.setEnabled(true);
		button.setBorder(BorderFactory.createEmptyBorder());
		return button;
	}
	private Icon createIcon(String file){
		String path = "/resources/images/toolbar/"+file;
		Icon icon = new ImageIcon(getClass().getResource(path));
		return icon;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnClient){
			if(clientFrame == null){
				clientFrame = new ClientFrame(controller);
				model.registerObserver((ClientObserver)clientFrame);
			}
			((ClientFrame) clientFrame).setDisabledButtonEdit();
			clientFrame.setVisible(true);
		}
		if(e.getSource() == btnCar){
			if(carFrame == null){
				carFrame = new CarFrame(controller);
				model.registerObserver((CarObserver)carFrame);
			}
			((CarFrame) carFrame).setDisabledButtonEditArchive();
			carFrame.setVisible(true);
		}
		if(e.getSource() == btnTransaction){
			if(transactionFrame == null){
				transactionFrame = new TransactionFrame(controller);
				model.registerObserver((TransactionObserver)transactionFrame);
				model.registerObserver((CarObserver)transactionFrame);
				model.registerObserver((ClientObserver)transactionFrame);
			}
			transactionFrame.setVisible(true);
		}
		if(e.getSource() == btnBooking){
			if(bookingFrame == null){
				bookingFrame = new BookingFrame(controller);
				model.registerObserver((BookingObserver)bookingFrame);
			}
			bookingFrame.setVisible(true);
		}
		if(e.getSource() == btnRepair){
			if(repairFrame == null){
				repairFrame = new RepairFrame(controller);
				model.registerObserver((CarObserver)repairFrame);
				model.registerObserver((ClientObserver)repairFrame);
			}
			repairFrame.setVisible(true);
		}
		if(e.getSource() == btnLogOut){
			// TODO wylogowanie workera
		}
		if(e.getSource() == btnExit){
			view.dispose();
			System.exit(0);
		}
	}



}
